package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.Key_ValueEntity;
import com.argrace.smart.entity.MineZigBeeDeviceListEntity;
import com.argrace.smart.glide.GliderImageLoaderUtil;
import com.argrace.smart.model.LocalDb_QingJing_DeviceTable;
import com.argrace.smart.ui.DrawableSwitch;
import com.argrace.smart.util.AlinkHttpUtil;
import com.argrace.smart.util.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by argrace_dev_1 on 2018/2/9.
 */

public class DeviceControl_KaiguanActivity extends BaseActivity {
    private ImageView img;
    private DrawableSwitch kaiguan_1, kaiguan_2, kaiguan_3;
    private DrawableSwitch kaiguan_4, kaiguan_5, kaiguan_6;
    private TextView text1, text2, text3, text4, text5, text6;
    MineZigBeeDeviceListEntity bean;
    private LinearLayout ll1, ll2, ll3, ll4, ll5, ll6;
    private Map<String, String> model_imgurl = new HashMap<>();
    private int type = ConstantConfigure.TYPE_CONTROL;
    private String name;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devicecontrol_kaiguan);
        init();
    }

    private void init() {
        bean = (MineZigBeeDeviceListEntity) getIntent().getSerializableExtra("deviceinfo");
        name = getIntent().getStringExtra("name");
        type = getIntent().getIntExtra("type", ConstantConfigure.TYPE_CONTROL);
        setTitle(true, false, bean.getName());
        if (type == ConstantConfigure.TYPE_SETTING) {
            //进入设备修改保存状态界面
            showRight(true, R.mipmap.head_title_tick);
            setLeftandRightClickListeners(null, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String str_params = translateParams();
                    LocalDb_QingJing_DeviceTable entity = new LocalDb_QingJing_DeviceTable();
                    entity.setJson_params(str_params);
                    entity.updateAll("uuid = ? and LocalDb_DeviceTable_deviceId = ?", bean.getUuid(), name);
                    finish();
                }
            });
        } else {
            showRight(true, R.mipmap.icon_modify);
            //进入设备修改界面
            setLeftandRightClickListeners(null, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_KaiguanActivity.this, DeviceModifyActivity.class);
                    intent.putExtra("uuid", bean.getUuid());
                    intent.putExtra("model", bean.getModel());
                    startActivity(intent);
                }
            });
        }

        setView();
        getDeviceDetailInfo();
    }

    private void setView() {
        img = (ImageView) findViewById(R.id.activity_devicecontrol_img);
        kaiguan_1 = (DrawableSwitch) findViewById(R.id.activity_devicecontrol_kaiguan_kaiguan1);
        kaiguan_2 = (DrawableSwitch) findViewById(R.id.activity_devicecontrol_kaiguan_kaiguan2);
        kaiguan_3 = (DrawableSwitch) findViewById(R.id.activity_devicecontrol_kaiguan_kaiguan3);
        kaiguan_4 = (DrawableSwitch) findViewById(R.id.activity_devicecontrol_kaiguan_kaiguan4);
        kaiguan_5 = (DrawableSwitch) findViewById(R.id.activity_devicecontrol_kaiguan_kaiguan5);
        kaiguan_6 = (DrawableSwitch) findViewById(R.id.activity_devicecontrol_kaiguan_kaiguan6);
        text1 = (TextView) findViewById(R.id.activity_devicecontrol_kaiguan_text1);
        text2 = (TextView) findViewById(R.id.activity_devicecontrol_kaiguan_text2);
        text3 = (TextView) findViewById(R.id.activity_devicecontrol_kaiguan_text3);
        text4 = (TextView) findViewById(R.id.activity_devicecontrol_kaiguan_text4);
        text5 = (TextView) findViewById(R.id.activity_devicecontrol_kaiguan_text5);
        text6 = (TextView) findViewById(R.id.activity_devicecontrol_kaiguan_text6);
        ll1 = (LinearLayout) findViewById(R.id.activity_devicecontrol_kaiguan_ll1);
        ll2 = (LinearLayout) findViewById(R.id.activity_devicecontrol_kaiguan_ll2);
        ll3 = (LinearLayout) findViewById(R.id.activity_devicecontrol_kaiguan_ll3);
        ll4 = (LinearLayout) findViewById(R.id.activity_devicecontrol_kaiguan_ll4);
        ll5 = (LinearLayout) findViewById(R.id.activity_devicecontrol_kaiguan_ll5);
        ll6 = (LinearLayout) findViewById(R.id.activity_devicecontrol_kaiguan_ll6);
        String imgurl = null;
        if (bean.getModel().equals(ConstantConfigure.MODEL_SANWEIDUOKONG)) {
            imgurl = ConstantConfigure.IMGURL_SANWEIQINGCHUDUOKONG_00501E13;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.VISIBLE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            kaiguan_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int state = kaiguan_1.isSwitchOn() ? 0 : 1;
                    kaiguan_1.setSwitchOn(!kaiguan_1.isSwitchOn());
                    kaiguan_1.invalidate();
                    sendMsg("Switch_1", String.valueOf(state));
                }
            });
            kaiguan_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int state = kaiguan_2.isSwitchOn() ? 0 : 1;
                    kaiguan_2.setSwitchOn(!kaiguan_2.isSwitchOn());
                    kaiguan_2.invalidate();
                    sendMsg("Switch_2", String.valueOf(state));
                }
            });
            kaiguan_3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int state = kaiguan_3.isSwitchOn() ? 0 : 1;
                    kaiguan_3.setSwitchOn(!kaiguan_3.isSwitchOn());
                    kaiguan_3.invalidate();
                    sendMsg("Switch_3", String.valueOf(state));
                }
            });
        }

        if (bean.getModel().equals(ConstantConfigure.MODEL_LIANGWEIDUOKONG)) {
            imgurl = ConstantConfigure.IMGURL_LIANGWEIQINGCHUDUOKONG_00501d7e;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.GONE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            kaiguan_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int state = kaiguan_1.isSwitchOn() ? 0 : 1;
                    kaiguan_1.setSwitchOn(!kaiguan_1.isSwitchOn());
                    kaiguan_1.invalidate();
                    sendMsg("Switch_1", String.valueOf(state));
                }
            });
            kaiguan_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int state = kaiguan_2.isSwitchOn() ? 0 : 1;
                    kaiguan_2.setSwitchOn(!kaiguan_2.isSwitchOn());
                    kaiguan_2.invalidate();
                    sendMsg("Switch_2", String.valueOf(state));
                }
            });
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_YIWEIDUOKONG)) {
            imgurl = ConstantConfigure.IMGURL_YIWEIQINGCHUDUOKONG_00501E11;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.GONE);
            ll3.setVisibility(View.GONE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            kaiguan_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int state = kaiguan_1.isSwitchOn() ? 0 : 1;
                    kaiguan_1.setSwitchOn(!kaiguan_1.isSwitchOn());
                    kaiguan_1.invalidate();
                    sendMsg("Switch_1", String.valueOf(state));
                }
            });
        }

        if (bean.getModel().equals(ConstantConfigure.MODEL_CHANGJINGKAIGUAN)) {
            imgurl = ConstantConfigure.IMGURL_CHANGJINGKAIGUAN_005F1DCD;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.VISIBLE);
            ll4.setVisibility(View.VISIBLE);
            ll5.setVisibility(View.VISIBLE);
            ll6.setVisibility(View.VISIBLE);
            kaiguan_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendMsg("KeyFobValue", String.valueOf(1));
                }
            });
            kaiguan_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendMsg("KeyFobValue", String.valueOf(2));
                }
            });
            kaiguan_3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendMsg("KeyFobValue", String.valueOf(3));
                }
            });
            kaiguan_4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendMsg("KeyFobValue", String.valueOf(4));
                }
            });
            kaiguan_5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendMsg("KeyFobValue", String.valueOf(5));
                }
            });
            kaiguan_6.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendMsg("KeyFobValue", String.valueOf(6));
                }
            });
        }

        if (imgurl != null) {
            GliderImageLoaderUtil.load(this, imgurl, img);
        }
    }

    //查询设备最新状态
    private void getDeviceDetailInfo() {
        String method = "mtop.openalink.app.core.device.get.status";
        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", bean.getUuid());
        AlinkHttpUtil.AlinkHttpRequest(method, params, new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                if (transitoryResponse == null || transitoryResponse.data == null) {
                    return;
                }
                Log.i("info", "子设备最新状态成功" + transitoryResponse.data);

                JSONObject obj = JSON.parseObject(transitoryResponse.data.toString());
                fillData(obj, obj.getString("uuid"));
            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                Log.i("info", "子设备最新状态失败" + aError.getMsg() + aError.getCode());
            }
        });
    }

    private void sendMsg(String key, String value) {
        if (type == ConstantConfigure.TYPE_SETTING) {
            return;
        }
        String method = "mtop.openalink.app.core.device.set.status";
        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", bean.getUuid());
        params.put("setParams", "{\"" + key + "\":\"" + value + "\"}");
        AlinkHttpUtil.AlinkHttpRequest(method, params, new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                Log.i("info", "发送消息成功" + transitoryResponse.data);
//                getDeviceDetailInfo();
            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                Log.i("info", "发送消息失败");
            }
        });
    }

    @Override
    protected void dorecieve(JSONObject obj) {
        super.dorecieve(obj);
        if (type == ConstantConfigure.TYPE_SETTING) {
            return;
        }
        if (obj == null || obj.getJSONObject("params") == null || obj.getJSONObject("params").getJSONObject("data") == null || obj.getJSONObject("params").getString("uuid") == null) {
            return;
        }
        fillData(obj.getJSONObject("params").getJSONObject("data"), obj.getJSONObject("params").getString("uuid"));
    }

    private void fillData(JSONObject obj, String uuid) {
        if (uuid == null || !uuid.equals(bean.getUuid())) {
            return;
        }

        if (bean.getModel().equals(ConstantConfigure.MODEL_YIWEIDUOKONG)) {
            Key_ValueEntity bean = JSON.parseObject(obj.getString("Switch"), Key_ValueEntity.class);
            //0关闭，1开启
            if (bean.getValue().equals("0")) {
                kaiguan_1.setSwitchOn(false);
            } else {
                kaiguan_1.setSwitchOn(true);
            }
            kaiguan_1.invalidate();
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_LIANGWEIDUOKONG)) {
            Key_ValueEntity bean1 = JSON.parseObject(obj.getString("Switch_1"), Key_ValueEntity.class);
            //0关闭，1开启
            if (bean1 != null && bean1.getValue().equals("0")) {
                kaiguan_1.setSwitchOn(false);
            } else {
                kaiguan_1.setSwitchOn(true);
            }
            kaiguan_1.invalidate();
            Key_ValueEntity bean2 = JSON.parseObject(obj.getString("Switch_2"), Key_ValueEntity.class);
            //0关闭，1开启
            if (bean2 != null && bean2.getValue().equals("0")) {
                kaiguan_2.setSwitchOn(false);
            } else {
                kaiguan_2.setSwitchOn(true);
            }
            kaiguan_2.invalidate();
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_SANWEIDUOKONG)) {
            Key_ValueEntity bean1 = JSON.parseObject(obj.getString("Switch_1"), Key_ValueEntity.class);
            //0关闭，1开启
            if (bean1 != null && bean1.getValue().equals("0")) {
                kaiguan_1.setSwitchOn(false);
            } else {
                kaiguan_1.setSwitchOn(true);
            }
            kaiguan_1.invalidate();
            Key_ValueEntity bean2 = JSON.parseObject(obj.getString("Switch_2"), Key_ValueEntity.class);
            //0关闭，1开启
            if (bean2 != null && bean2.getValue().equals("0")) {
                kaiguan_2.setSwitchOn(false);
            } else {
                kaiguan_2.setSwitchOn(true);
            }
            kaiguan_2.invalidate();
            Key_ValueEntity bean3 = JSON.parseObject(obj.getString("Switch_3"), Key_ValueEntity.class);
            //0关闭，1开启
            if (bean3 != null && bean3.getValue().equals("0")) {
                kaiguan_3.setSwitchOn(false);
            } else {
                kaiguan_3.setSwitchOn(true);
            }
            kaiguan_3.invalidate();
        }

        if (bean.getModel().equals(ConstantConfigure.MODEL_CHANGJINGKAIGUAN)) {
            Key_ValueEntity bean1 = JSON.parseObject(obj.getString("KeyFobValue"), Key_ValueEntity.class);
            if (bean1 != null && bean1.getValue().equals("1")) {
                kaiguan_1.setSwitchOn(true);
                kaiguan_2.setSwitchOn(false);
                kaiguan_3.setSwitchOn(false);
                kaiguan_4.setSwitchOn(false);
                kaiguan_5.setSwitchOn(false);
                kaiguan_6.setSwitchOn(false);
            }
            if (bean1.getValue().equals("2")) {
                kaiguan_1.setSwitchOn(false);
                kaiguan_2.setSwitchOn(true);
                kaiguan_3.setSwitchOn(false);
                kaiguan_4.setSwitchOn(false);
                kaiguan_5.setSwitchOn(false);
                kaiguan_6.setSwitchOn(false);
            }
            if (bean1.getValue().equals("3")) {
                kaiguan_3.setSwitchOn(true);
                kaiguan_2.setSwitchOn(false);
                kaiguan_1.setSwitchOn(false);
                kaiguan_4.setSwitchOn(false);
                kaiguan_5.setSwitchOn(false);
                kaiguan_6.setSwitchOn(false);
            }
            if (bean1.getValue().equals("4")) {
                kaiguan_4.setSwitchOn(true);
                kaiguan_2.setSwitchOn(false);
                kaiguan_3.setSwitchOn(false);
                kaiguan_1.setSwitchOn(false);
                kaiguan_5.setSwitchOn(false);
                kaiguan_6.setSwitchOn(false);
            }
            if (bean1.getValue().equals("5")) {
                kaiguan_5.setSwitchOn(true);
                kaiguan_2.setSwitchOn(false);
                kaiguan_3.setSwitchOn(false);
                kaiguan_4.setSwitchOn(false);
                kaiguan_1.setSwitchOn(false);
                kaiguan_6.setSwitchOn(false);
            }
            if (bean1.getValue().equals("6")) {
                kaiguan_6.setSwitchOn(true);
                kaiguan_2.setSwitchOn(false);
                kaiguan_3.setSwitchOn(false);
                kaiguan_4.setSwitchOn(false);
                kaiguan_5.setSwitchOn(false);
                kaiguan_1.setSwitchOn(false);
            }
            kaiguan_6.invalidate();
            kaiguan_2.invalidate();
            kaiguan_3.invalidate();
            kaiguan_4.invalidate();
            kaiguan_5.invalidate();
            kaiguan_1.invalidate();
        }
    }

    private String translateParams() {
        //0关闭，1开启
        Map<String, String> params = new HashMap<>();
        if (bean.getModel().equals(ConstantConfigure.MODEL_YIWEIDUOKONG)) {
            if (kaiguan_1.isSwitchOn()) {
                params.put("Switch", "1");
            } else {
                params.put("Switch", "0");
            }
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_LIANGWEIDUOKONG)) {
            if (kaiguan_1.isSwitchOn()) {
                params.put("Switch_1", "1");
            } else {
                params.put("Switch_1", "0");
            }
            if (kaiguan_2.isSwitchOn()) {
                params.put("Switch_2", "1");
            } else {
                params.put("Switch_2", "0");
            }
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_SANWEIDUOKONG)) {
            if (kaiguan_1.isSwitchOn()) {
                params.put("Switch_1", "1");
            } else {
                params.put("Switch_1", "0");
            }
            if (kaiguan_2.isSwitchOn()) {
                params.put("Switch_2", "1");
            } else {
                params.put("Switch_2", "0");
            }
            if (kaiguan_3.isSwitchOn()) {
                params.put("Switch_3", "1");
            } else {
                params.put("Switch_3", "0");
            }
        }
        return utils.getTheJson(params);
    }
}
